package diva.pap.transactions;

import static diva.lib.lang.Lang.info;
import static diva.lib.lang.Lang.map;
import static diva.lib.lang.Lang.no;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import diva.android_library.ActivityPresenter;
import diva.android_library.rpc.Response;
import diva.lib.collections.Map;
import diva.lib.event.Event;
import diva.lib.event.Listener;
import diva.pap.R;
import diva.pap.TransactionsRequest;
import diva.pap.model.Application;

public abstract class TransactionsPresenterBase extends ActivityPresenter {

  private class ListAdapter extends BaseAdapter {
    private final int itemsCount;
    private final Map<Integer, TransactionsRequest> requests = map();
    private final String fromtime;

    public ListAdapter(TransactionsRequest request) {
      itemsCount = request.getAllItemsCount();
      fromtime = request.getFromTime();
      requests.put(0, request);
    }

    public int getCount() {
      return itemsCount;
    }

    public Object getItem(int position) {
      return position;
    }

    public long getItemId(int position) {
      return position;
    }

    public View getView(int position, View view, ViewGroup parent) {
      TransactionRow row;
      if (no(view)) {
        view = inflateLayout(row_layout_id);
        view.setTag(row = createTransactionRow());
        row.initialize(view);
      } else row = (TransactionRow) view.getTag();
      int rowIndexInRequest = position - position / 30 * 30;
      info(position, itemsCount);
      row.load(rowIndexInRequest, getRequestFor(position));
      return view;
    }

    private TransactionsRequest getRequestFor(final int position) {
      int requestIndex = position / 30;
      TransactionsRequest request;
      if (requests.hasKey(requestIndex))
        request = requests.value(requestIndex);
      else {
        request = new TransactionsRequest();
        request.initialize(requestIndex * 30, requestIndex * 30 + 30, fromtime);
        request.send();
        requests.put(requestIndex, request);
      }
      return request;
    }
  }

  private final int row_layout_id;

  public TransactionsPresenterBase(Activity activity, int row_layout_id) {
    super(activity);
    this.row_layout_id = row_layout_id;
  }

  public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.transactions);
    load();
    getButton(R.id.button_reload).setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        Application.getInstance().reload();
      }
    });
    attach(Application.getInstance().getEventReload(), new Listener<Void>() {
      public void onEvent(Event<Void> event, Void arg) {
        load();
      }
    });
  }

  protected abstract TransactionRow createTransactionRow();

  private void load() {
    TransactionsRequest request = new TransactionsRequest();
    request.setResponse(new Response<TransactionsRequest>() {
      public void onReuestDone(TransactionsRequest request) {
        if (request.getAllItemsCount() > 0)
          getListView(R.id.transactions_list).setAdapter(new ListAdapter(request));
      }

      public void onReuestFailed() {}
    });
    request.initialize(activity, 0, 30);
    request.send();
  }

}
